package com.dj.shop.controller;

import com.alibaba.fastjson.JSON;
import com.dj.shop.model.*;
import com.dj.shop.poi.impl.FileInterfaceImpl;
import com.dj.shop.service.OrderService;
import com.dj.shop.util.FileUtil;
import com.dj.shop.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author renshp
 * @Title: OrderController
 * @Description: TODO
 * @date 2019/3/5
 */
@Controller
@RequestMapping("/web/order/v1")
public class OrderController extends BaseController{

    @Autowired
    OrderService orderService;
/*
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public @ResponseBody
    Result addOrder(Order order){

        if (orderService.addOrder(order.getOrderFlow(), order.getTransFlowList()) == -1){
            return Result.error();
        }

        return Result.success();

    }*/

    @RequestMapping(value = "/searchOrders", method = RequestMethod.GET)
    public @ResponseBody Result searchOrders(Map<String, String> map){
        System.out.println(map.get("startDate"));
        return Result.success();
    }

    /***
     * 根据订单流水号查询订单信息
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/queryOrderByOrderNo", method =RequestMethod.GET)
    public @ResponseBody Result queryOrderById(int orderNo){

        OrderFlow orderFlow = orderService.queryOrderById(orderNo);
        if (orderFlow == null){
            return Result.error();
        }

        return Result.success(orderFlow);
    }

    /***
     * 根据订单流水号查询订单商品清单
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/queryGoodsListByOrderNo", method = RequestMethod.GET)
    public @ResponseBody Result queryGoodsListByOrderNo(int orderNo){

        List<GoodsFlow> goodsFlowList = orderService.queryGoodsListByOrderNo(orderNo);

        if (goodsFlowList.size() == 0){
            return Result.error();
        }

        return Result.success(goodsFlowList);
    }

    /***
     * 更新订单状态
     * @param orderNo
     * @param orderStatus
     * @return
     */
    @RequestMapping(value = "/updateOrderStatus", method = RequestMethod.POST)
    public @ResponseBody Result updateOrderStatus(int orderNo, String orderStatus){

        if (orderService.updateOrderStatus(orderNo, orderStatus) < 1){
            return Result.error();
        }

        return Result.success();
    }

    /***
     * 订单组合查询
     */
    @RequestMapping(value = "/queryOrder", method = RequestMethod.POST)
    public @ResponseBody Result queryOrder(HttpServletRequest request) {
        Map<String, Object> param = getParam(request);

        List<OrderWrapper> orderList = orderService.queryOrder(param);
        return Result.success(orderList);
    }


//    @RequestMapping("/upload")
//    public void upload(MultipartFile file, HttpServletResponse response) throws IOException {
//        //注意头部内容必须和对象成员名字一致
//        //返回所有excel对象的set集合
//        //对象中不要出现非基本类型成员（String 除外）
//       // String[] header={"name","phone","type","dept","number","idcard","birthDay","nickName","student"};
//        String[] header = {"className"};
//        Set<Test1> rowCell= FileUtil.transFile(file,new FileInterfaceImpl(),header,Test1.class);
//        System.out.println( JSON.toJSON(rowCell));
//    }


}
